package com.zueb.service.impl;

import com.zueb.mapper.VaccinationMapper;
import com.zueb.pojo.Vaccination;
import com.zueb.service.VaccinationService;
import com.zueb.util.PageUtils;
import com.zueb.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class VaccinationServiceImpl implements VaccinationService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public int addVaccination(Vaccination vaccination) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        VaccinationMapper vaccinationMapper = sqlSession.getMapper(VaccinationMapper.class);
        //调用方法
        int flag = vaccinationMapper.addVaccination(vaccination);
        //提交事务
        sqlSession.commit();
        //释放连接资源
        sqlSession.close();
        //返回参数
        return flag;
    }

    @Override
    public List<Vaccination> showVaccination(String identification) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        VaccinationMapper vaccinationMapper = sqlSession.getMapper(VaccinationMapper.class);
        //调用方法
        List<Vaccination> vaccinations = vaccinationMapper.showVaccination(identification);

        //关闭连接资源
        sqlSession.close();
        //返回参数
        return vaccinations;
    }

    @Override
    public List<Vaccination> showAllVaccinationByPage(Integer curPage, Integer pageSize, Integer facultyId, Integer classId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        VaccinationMapper vaccinationMapper = sqlSession.getMapper(VaccinationMapper.class);
        //调用方法
        int index = PageUtils.getIndex(curPage, pageSize);
        List<Vaccination> vaccinations = vaccinationMapper.showAllVaccinationByPage(index, pageSize, facultyId, classId);
        //关闭连接资源
        sqlSession.close();
        //返回参数
        return vaccinations;
    }

    @Override
    public int showAllVaccinationCount(Integer facultyId, Integer classId) {
        //获取SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //获取Mapper
        VaccinationMapper vaccinationMapper = sqlSession.getMapper(VaccinationMapper.class);
        //调用方法
        int count = vaccinationMapper.showAllVaccinationCount(facultyId, classId);
        //关闭连接资源
        sqlSession.close();
        //返回参数
        return count;
    }

}
